package com.yswl.pri.home.vedio;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.yswl.pri.R;
import com.yswl.pri.common.ToolbarActivity;
import com.yswl.pri.picture.SpacesItemDecoration;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import yswl.com.klibrary.base.BaseRecyclerAdapter;
import yswl.com.klibrary.base.BaseViewHolder;
import yswl.com.klibrary.http.CallBack.HttpCallback;
import yswl.com.klibrary.http.HttpClientProxy2;
import yswl.com.klibrary.util.L;

/**
 * 搜索种子详情
 */
public class MusicDetailActivity extends ToolbarActivity implements HttpCallback<String> {

    private static final String TAG = "MusicDetailActivity";

    public static void JumpAct(Activity context, String search, String url) {
        Intent intent = new Intent(context, MusicDetailActivity.class);
        intent.putExtra("search", search);
        intent.putExtra("link", url);
        context.startActivity(intent);
    }

    TextView nameTV;
    TextView sizeTV;
    TextView timeTV;
    TextView magnetTV;
    TextView seedTV;
    TextView thunderTV;
    MusicDetailListAdapter movieSearchListAdapter;
    public String key;
    public String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_detail);
        key = getIntent().getStringExtra("search");
        url = getIntent().getStringExtra("link");
        setToolbarTitle(key + "  搜索 ");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        nameTV = findViewById(R.id.tv_name);
        sizeTV = findViewById(R.id.tv_size);
        timeTV = findViewById(R.id.tv_time);
        magnetTV = findViewById(R.id.tv_magnet_link);
        magnetTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String link = ((TextView) v).getText().toString();

            }
        });
        thunderTV = findViewById(R.id.tv_thunder_link);
        seedTV = findViewById(R.id.tv_seed_link);

        RecyclerView recyclerView = findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        SpacesItemDecoration decoration = new SpacesItemDecoration(5);
        recyclerView.addItemDecoration(decoration);
        recyclerView.setNestedScrollingEnabled(false);
        movieSearchListAdapter = new MusicDetailListAdapter(this, null, R.layout.activity_music_detail_list_item);
        recyclerView.setAdapter(movieSearchListAdapter);
        loadResource(url);

    }


    void loadResource(String url) {
        if (!TextUtils.isEmpty(url))
            HttpClientProxy2.getInstance().getAsyn(url, 1, null, this);
    }


    @Override
    public void onSucceed(int requestId, String result) {
        L.d(TAG, " onSucceed : " + result);

        Document document = Jsoup.parse(result);
        String title = document.title();
        L.d(TAG, " title : " + title);

        Element bady = document.body();
        Elements resous = bady.select("#wall > .res-title");
        if (resous == null || resous.size() == 0) return;
        String name = resous.text();
        L.d(TAG, " name : " + name);
        nameTV.setText(name);

        Elements fileDerail = bady.select(".fileDetail p");
        if (fileDerail != null && fileDerail.size() >3) {
            String size = fileDerail.get(1).html();
            String time = fileDerail.get(2).html();
            timeTV.setText(time);
            sizeTV.setText(size);
        }
        Elements magnetDetail = bady.select(".fileDetail #down-link");
       String magnetLink =  magnetDetail.get(0).attr("href");
        magnetTV.setText(magnetLink);
        seedTV.setText("无");
        thunderTV.setText("无");

        Elements filesE = bady.select("ol li");
        if (filesE != null && filesE.size() > 0) {
            List<String> files = new ArrayList<>();
            for (int i = 0; i < filesE.size(); i++) {
                String file = filesE.get(i).text();
                files.add(file);
            }
            movieSearchListAdapter.setList(files);
        }


    }

    @Override
    public void onFail(int requestId, String errorMsg) {
        L.e(TAG, " onFail : " + errorMsg);
    }


    public class MusicDetailListAdapter extends BaseRecyclerAdapter<String> {

        public MusicDetailListAdapter(Context mContext, List<String> mList, int itemLayoutId) {
            super(mContext, mList, itemLayoutId);
        }

        @Override
        public void onBind(BaseViewHolder holder, final String name, int position) {
            TextView textView = holder.findById(R.id.name);
            textView.setText(name);
        }
    }
}
